﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class Statement : BasePage
    {
        protected Button btnConfirm;
        protected Button btnDelete;
        protected Button btnExport;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnRevertConfirm;
        protected Button btnSave;
        protected Button btnSearch;
        protected CheckBox chkAccessorial;
        protected CheckBox chkInBound;
        protected CheckBox chkInventory;
        protected CheckBox chkMovement;
        protected CheckBox chkOutBound;
        protected CheckBox chkStorageCharges;
        protected CheckBox chkVas;
        protected DropDownList ddlBillingObject;
        protected DropDownList ddlDueFromTo;
        protected DropDownList ddlStatus;
        protected HtmlForm F9201;
        protected GridView gvStatementBillingCredence;
        protected GridView gvStatementDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnStatementID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected Label Label1;
        protected Label lblBillingObject;
        protected Label lblBillingObjectID;
        protected Label lblDueFromTo;
        protected Label lblEndTime;
        protected Label lblInvoiceAmount;
        protected Label lblLastInvoiceAmount;
        protected Label lblLastPayAmount;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblPayAmount;
        protected Label lblPaymentDate;
        protected Label lblRemark;
        protected Label lblStatementID;
        protected Label lblTotalAmount;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBillingObject;
        protected TextBox txtEndTime;
        protected TextBox txtInvoiceAmount;
        protected TextBox txtLastInvoiceAmount;
        protected TextBox txtLastPayAmount;
        protected TextBox txtPayAmount;
        protected TextBox txtPaymentDate;
        protected TextBox txtRemark;
        protected TextBox txtStatementID;
        protected TextBox txtTotalAmount;
        protected UpdatePanel up1;

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                if (this.ddlStatus.SelectedValue != "New")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9200_024");
                }
                else
                {
                    this.UpdateStatementInfo("Reviewed");
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                if (this.ddlStatus.SelectedValue != "New")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9200_023");
                }
                else
                {
                    this.lblMsg.Text = string.Empty;
                    List<string> statementlist = new List<string> {
                        this.txtStatementID.Text.Trim()
                    };
                    if (string.IsNullOrEmpty(BillingServiceClientProxy.GetInstance().DeleteStatement(statementlist)))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9200_015");
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9200_014");
                        base.Response.Redirect("Statement.aspx?from=" + this.hdnFrom.Value, false);
                    }
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtStatementID.Text.Trim()) && !this.txtStatementID.Enabled)
            {
                DataSet ds = base.DataEntityViewState as DataSet;
                try
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.gvStatementDetail.PageSize = 0x186a0;
                        base.BindData2GridView(this.gvStatementDetail, ds);
                        base.Export2Excel("StatementDetail.xls", this.gvStatementDetail);
                        this.gvStatementDetail.PageSize = 50;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
                finally
                {
                    this.gvStatementBillingCredence.DataSource = null;
                    this.gvStatementBillingCredence.DataBind();
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("StatementQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("StatementList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            string from = this.hdnFrom.Value;
            base.Response.Redirect("Statement.aspx?from=" + from, false);
        }

        protected void btnRevertConfirm_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                if (this.ddlStatus.SelectedValue != "Reviewed")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9200_025");
                }
                else
                {
                    this.UpdateStatementInfo("New");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue != "New")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_022");
            }
            else
            {
                this.InsertStatementInfo();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
            this.Session["BillingCredenceListInfo"] = null;
            if (string.IsNullOrEmpty(this.txtBillingObject.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_017");
            }
            else if (string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_020");
            }
            else
            {
                DateTime dt = new DateTime();
                if (!DateTime.TryParse(this.txtEndTime.Text.Trim(), out dt))
                {
                    this.txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                string statementType = string.Empty;
                if (this.chkInBound.Checked)
                {
                    statementType = statementType + "InBound,";
                }
                if (this.chkOutBound.Checked)
                {
                    statementType = statementType + "OutBound,";
                }
                if (this.chkMovement.Checked)
                {
                    statementType = statementType + "Movement,";
                }
                if (this.chkVas.Checked)
                {
                    statementType = statementType + "Vas,";
                }
                if (this.chkInventory.Checked)
                {
                    statementType = statementType + "InvCharges,";
                }
                if (this.chkAccessorial.Checked)
                {
                    statementType = statementType + "AccessorialCharges,";
                }
                if (this.chkStorageCharges.Checked)
                {
                    statementType = statementType + "StorageCharges,";
                }
                if (string.IsNullOrEmpty(statementType))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9200_018");
                }
                else
                {
                    string endDate = Convert.ToDateTime(this.txtEndTime.Text.Trim()).ToString() + " 23:59:59";
                    BillingCredenceQueryEntity billingCredenceQueryEntity = new BillingCredenceQueryEntity {
                        DueFromTo = base.GetParamsString(this.ddlDueFromTo.SelectedValue, "Same As"),
                        BillingObject = base.GetParamsString(this.ddlBillingObject.SelectedValue, "Same As"),
                        BillingObjectID = base.GetParamsString(this.txtBillingObject.Text.Trim(), "Same As"),
                        CreatedDate = base.GetParamsString(endDate, "Before", "", "CreatedDate"),
                        CredenceType = base.GetParamsString(statementType.Substring(0, statementType.Length - 1), "One Of"),
                        IsStatement = base.GetParamsString("Y", "Not Same As"),
                        IsGetAll = true
                    };
                    DataSet ds = BillingServiceClientProxy.GetInstance().GetBillingCredenceGroupByQueryList(billingCredenceQueryEntity);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        base.BindData2GridView(this.gvStatementBillingCredence, ds);
                        this.btnSave.Enabled = true;
                        this.ddlDueFromTo.Enabled = false;
                        this.ddlBillingObject.Enabled = false;
                        this.txtBillingObject.Enabled = false;
                        this.txtEndTime.Enabled = false;
                        this.txtEndTime.ReadOnly = true;
                        this.img1.Visible = false;
                        this.img2.Visible = false;
                        base.DataEntityViewState = ds;
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
                    }
                    else
                    {
                        this.gvStatementBillingCredence.DataSource = null;
                        this.gvStatementBillingCredence.DataBind();
                    }
                }
            }
        }

        protected void ddlBillingObject_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void gvStatementBillingCredence_Init(object sender, EventArgs e)
        {
        }

        protected void gvStatementBillingCredence_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.RememberOldValues(this.gvStatementBillingCredence);
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = base.DataEntityViewState;
            (sender as GridView).DataBind();
            this.RePopulateValues(this.gvStatementBillingCredence);
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
        }

        protected void gvStatementBillingCredence_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvStatementBillingCredence.PageIndex * this.gvStatementBillingCredence.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvStatementBillingCredence_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                DataSet ds = base.DataEntityViewState as DataSet;
                DataView dv = ds.Tables[0].DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvStatementBillingCredence, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void gvStatementDetail_Init(object sender, EventArgs e)
        {
        }

        protected void gvStatementDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.RememberOldValues(this.gvStatementDetail);
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = base.DataEntityViewState;
            (sender as GridView).DataBind();
            this.RePopulateValues(this.gvStatementDetail);
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
        }

        protected void gvStatementDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvStatementDetail.PageIndex * this.gvStatementDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvStatementDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                DataSet ds = base.DataEntityViewState as DataSet;
                DataView dv = ds.Tables[0].DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvStatementDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
            }
        }

        private void InsertStatementInfo()
        {
            try
            {
                List<BillingCredenceInfo> billingCredenceListInfo;
                int i;
                BillingCredenceInfo billingCredenceInfo;
                StatementInfo StatementInfo = new StatementInfo {
                    StatementID = this.txtStatementID.Text,
                    Status = "New"
                };
                if (!string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
                {
                    StatementInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                }
                StatementInfo.Remark = this.txtRemark.Text;
                if (!string.IsNullOrEmpty(this.txtPaymentDate.Text.Trim()))
                {
                    StatementInfo.PaymentDate = DateTime.Now;
                }
                StatementInfo.CreatedBy = base.UserInfo.UserID;
                StatementInfo.DueFromTo = this.ddlDueFromTo.SelectedValue;
                StatementInfo.BillingObject = this.ddlBillingObject.SelectedValue;
                StatementInfo.BillingObjectID = this.txtBillingObject.Text.Trim();
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtTotalAmount.Text.Trim()))
                {
                    if (decimal.TryParse(this.txtTotalAmount.Text.Trim(), out tempDec))
                    {
                        StatementInfo.TotalAmount = decimal.Parse(this.txtTotalAmount.Text.Trim());
                    }
                    else
                    {
                        StatementInfo.TotalAmount = tempDec;
                    }
                }
                if (string.IsNullOrEmpty(this.txtInvoiceAmount.Text.Trim()))
                {
                    StatementInfo.InvoiceAmount = 0M;
                }
                else
                {
                    StatementInfo.InvoiceAmount = decimal.Parse(this.txtInvoiceAmount.Text.Trim());
                }
                if (string.IsNullOrEmpty(this.txtPayAmount.Text.Trim()))
                {
                    StatementInfo.PayAmount = 0M;
                }
                else
                {
                    StatementInfo.PayAmount = decimal.Parse(this.txtPayAmount.Text.Trim());
                }
                decimal totalAmount = 0M;
                List<BillingCredenceInfo> selectBillingCredenceListInfo = new List<BillingCredenceInfo>();
                if (!string.IsNullOrEmpty(this.txtStatementID.Text.Trim()) && !this.txtStatementID.Enabled)
                {
                    this.RememberOldValues(this.gvStatementDetail);
                    if (this.Session["BillingCredenceListInfo"] != null)
                    {
                        billingCredenceListInfo = (List<BillingCredenceInfo>) this.Session["BillingCredenceListInfo"];
                        for (i = 0; i < billingCredenceListInfo.Count; i++)
                        {
                            billingCredenceInfo = new BillingCredenceInfo {
                                CredenceType = billingCredenceListInfo[i].CredenceType,
                                BillingObject = billingCredenceListInfo[i].BillingObject,
                                BillingObjectID = billingCredenceListInfo[i].BillingObjectID,
                                DueFromTo = billingCredenceListInfo[i].DueFromTo,
                                CostType = billingCredenceListInfo[i].CostType,
                                DocID = billingCredenceListInfo[i].DocID,
                                BillingAmount = billingCredenceListInfo[i].BillingAmount
                            };
                            totalAmount += billingCredenceListInfo[i].BillingAmount;
                            selectBillingCredenceListInfo.Add(billingCredenceInfo);
                        }
                    }
                    else
                    {
                        i = 0;
                        while (i < this.gvStatementBillingCredence.Rows.Count)
                        {
                            if (((CheckBox) this.gvStatementDetail.Rows[i].FindControl("chbSelect")).Checked)
                            {
                                billingCredenceInfo = new BillingCredenceInfo {
                                    CredenceType = this.gvStatementDetail.DataKeys[i]["CREDENCE_TYPE"].ToString(),
                                    BillingObject = this.gvStatementDetail.DataKeys[i]["BILLING_OBJECT"].ToString(),
                                    BillingObjectID = this.gvStatementDetail.DataKeys[i]["BILLING_OBJECT_ID"].ToString(),
                                    DueFromTo = this.gvStatementDetail.DataKeys[i]["DUE_FROM_TO"].ToString(),
                                    CostType = this.gvStatementDetail.DataKeys[i]["COST_TYPE"].ToString(),
                                    DocID = this.gvStatementDetail.DataKeys[i]["DOC_ID"].ToString(),
                                    BillingAmount = decimal.Parse(this.gvStatementDetail.DataKeys[i]["BILLING_AMOUNT"].ToString())
                                };
                                totalAmount += decimal.Parse(this.gvStatementDetail.DataKeys[i]["BILLING_AMOUNT"].ToString());
                                selectBillingCredenceListInfo.Add(billingCredenceInfo);
                            }
                            i++;
                        }
                    }
                }
                else
                {
                    this.RememberOldValues(this.gvStatementBillingCredence);
                    if (this.Session["BillingCredenceListInfo"] != null)
                    {
                        billingCredenceListInfo = (List<BillingCredenceInfo>) this.Session["BillingCredenceListInfo"];
                        for (i = 0; i < billingCredenceListInfo.Count; i++)
                        {
                            billingCredenceInfo = new BillingCredenceInfo {
                                CredenceType = billingCredenceListInfo[i].CredenceType,
                                BillingObject = billingCredenceListInfo[i].BillingObject,
                                BillingObjectID = billingCredenceListInfo[i].BillingObjectID,
                                DueFromTo = billingCredenceListInfo[i].DueFromTo,
                                CostType = billingCredenceListInfo[i].CostType,
                                DocID = billingCredenceListInfo[i].DocID,
                                BillingAmount = billingCredenceListInfo[i].BillingAmount
                            };
                            totalAmount += billingCredenceListInfo[i].BillingAmount;
                            selectBillingCredenceListInfo.Add(billingCredenceInfo);
                        }
                    }
                    else
                    {
                        i = 0;
                        while (i < this.gvStatementBillingCredence.Rows.Count)
                        {
                            if (((CheckBox) this.gvStatementBillingCredence.Rows[i].FindControl("chbSelect")).Checked)
                            {
                                billingCredenceInfo = new BillingCredenceInfo {
                                    CredenceType = this.gvStatementBillingCredence.DataKeys[i]["CREDENCE_TYPE"].ToString(),
                                    BillingObject = this.gvStatementBillingCredence.DataKeys[i]["BILLING_OBJECT"].ToString(),
                                    BillingObjectID = this.gvStatementBillingCredence.DataKeys[i]["BILLING_OBJECT_ID"].ToString(),
                                    DueFromTo = this.gvStatementBillingCredence.DataKeys[i]["DUE_FROM_TO"].ToString(),
                                    CostType = this.gvStatementBillingCredence.DataKeys[i]["COST_TYPE"].ToString(),
                                    DocID = this.gvStatementBillingCredence.DataKeys[i]["DOC_ID"].ToString(),
                                    BillingAmount = decimal.Parse(this.gvStatementBillingCredence.DataKeys[i]["BILLING_AMOUNT"].ToString())
                                };
                                totalAmount += decimal.Parse(this.gvStatementBillingCredence.DataKeys[i]["BILLING_AMOUNT"].ToString());
                                selectBillingCredenceListInfo.Add(billingCredenceInfo);
                            }
                            i++;
                        }
                    }
                }
                StatementInfo.TotalAmount = totalAmount;
                if ((selectBillingCredenceListInfo == null) || (selectBillingCredenceListInfo.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9200_019");
                }
                else
                {
                    StatementInfo.StatementDetailInfos = new List<StatementDetailInfo>();
                    for (i = 0; i < selectBillingCredenceListInfo.Count; i++)
                    {
                        StatementDetailInfo statementDetailInfo = new StatementDetailInfo {
                            CredenceType = selectBillingCredenceListInfo[i].CredenceType,
                            BillingObject = selectBillingCredenceListInfo[i].BillingObject,
                            BillingObjectID = selectBillingCredenceListInfo[i].BillingObjectID,
                            DueFromTo = selectBillingCredenceListInfo[i].DueFromTo,
                            CostType = selectBillingCredenceListInfo[i].CostType,
                            OrderID = selectBillingCredenceListInfo[i].DocID,
                            Receivable = selectBillingCredenceListInfo[i].BillingAmount
                        };
                        StatementInfo.StatementDetailInfos.Add(statementDetailInfo);
                    }
                    if (this.hdnFlag.Value != "1")
                    {
                        this.hdnStatementID.Value = BillingServiceClientProxy.GetInstance().InsertStatement(StatementInfo);
                    }
                    else
                    {
                        this.hdnStatementID.Value = BillingServiceClientProxy.GetInstance().UpdateStatement(StatementInfo);
                    }
                    this.hdnLineID.Value = "10";
                    this.hdnFlag.Value = "1";
                    base.DataEntityViewState = StatementInfo;
                    this.txtStatementID.Text = this.hdnStatementID.Value;
                    this.txtStatementID.Enabled = false;
                    this.Session["BillingCredenceListInfo"] = null;
                    this.SetStatementInfo2Form();
                    this.lblMsg.Text = this.hdnStatementID.Value + base.GetGlobalMsgResource("F9200_007");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_008") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "StatementStatu");
            base.BindData2DDLBySysCode(this.ddlBillingObject, "BillingObject");
            base.BindData2DDLBySysCode(this.ddlDueFromTo, "DueFromTo");
            this.Session["BillingCredenceListInfo"] = null;
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnStatementID.Value = base.Request.QueryString["ID"].ToString();
                this.SetStatementInfo2Form();
            }
            else
            {
                this.hdnStatementID.Value = "";
                this.SetStatementInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void RememberOldValues(GridView gvStatementBillingCredence)
        {
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            if (this.Session["BillingCredenceListInfo"] != null)
            {
                billingCredenceListInfo = (List<BillingCredenceInfo>) this.Session["BillingCredenceListInfo"];
            }
            for (int j = 0; j < gvStatementBillingCredence.Rows.Count; j++)
            {
                int i;
                string credenceType = gvStatementBillingCredence.DataKeys[j]["CREDENCE_TYPE"].ToString();
                string billingObject = gvStatementBillingCredence.DataKeys[j]["BILLING_OBJECT"].ToString();
                string billingObjectID = gvStatementBillingCredence.DataKeys[j]["BILLING_OBJECT_ID"].ToString();
                string dueFromTo = gvStatementBillingCredence.DataKeys[j]["DUE_FROM_TO"].ToString();
                string costType = gvStatementBillingCredence.DataKeys[j]["COST_TYPE"].ToString();
                string docID = gvStatementBillingCredence.DataKeys[j]["DOC_ID"].ToString();
                decimal billingAmount = decimal.Parse(gvStatementBillingCredence.DataKeys[j]["BILLING_AMOUNT"].ToString());
                if (((CheckBox) gvStatementBillingCredence.Rows[j].FindControl("chbSelect")).Checked)
                {
                    bool exist = false;
                    i = 0;
                    while (i < billingCredenceListInfo.Count)
                    {
                        if (((((billingCredenceListInfo[i].CredenceType == credenceType) && (billingCredenceListInfo[i].BillingObject == billingObject)) && ((billingCredenceListInfo[i].BillingObjectID == billingObjectID) && (billingCredenceListInfo[i].DueFromTo == dueFromTo))) && ((billingCredenceListInfo[i].CostType == costType) && (billingCredenceListInfo[i].DocID == docID))) && (billingCredenceListInfo[i].BillingAmount == billingAmount))
                        {
                            exist = true;
                            break;
                        }
                        i++;
                    }
                    if (!exist)
                    {
                        BillingCredenceInfo billingCredenceIno = new BillingCredenceInfo {
                            CredenceType = credenceType,
                            BillingObject = billingObject,
                            BillingObjectID = billingObjectID,
                            DueFromTo = dueFromTo,
                            CostType = costType,
                            DocID = docID,
                            BillingAmount = billingAmount
                        };
                        billingCredenceListInfo.Add(billingCredenceIno);
                    }
                    continue;
                }
                for (i = 0; i < billingCredenceListInfo.Count; i++)
                {
                    if (((((billingCredenceListInfo[i].CredenceType == credenceType) && (billingCredenceListInfo[i].BillingObject == billingObject)) && ((billingCredenceListInfo[i].BillingObjectID == billingObjectID) && (billingCredenceListInfo[i].DueFromTo == dueFromTo))) && ((billingCredenceListInfo[i].CostType == costType) && (billingCredenceListInfo[i].DocID == docID))) && (billingCredenceListInfo[i].BillingAmount == billingAmount))
                    {
                        billingCredenceListInfo.RemoveAt(i);
                    }
                }
            }
            if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
            {
                this.Session["BillingCredenceListInfo"] = billingCredenceListInfo;
            }
            else
            {
                this.Session["BillingCredenceListInfo"] = null;
            }
        }

        private void RePopulateValues(GridView gvStatementBillingCredence)
        {
            List<BillingCredenceInfo> billingCredenceListInfo = (List<BillingCredenceInfo>) this.Session["BillingCredenceListInfo"];
            if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
            {
                for (int j = 0; j < gvStatementBillingCredence.Rows.Count; j++)
                {
                    string credenceType = gvStatementBillingCredence.DataKeys[j]["CREDENCE_TYPE"].ToString();
                    string billingObject = gvStatementBillingCredence.DataKeys[j]["BILLING_OBJECT"].ToString();
                    string billingObjectID = gvStatementBillingCredence.DataKeys[j]["BILLING_OBJECT_ID"].ToString();
                    string dueFromTo = gvStatementBillingCredence.DataKeys[j]["DUE_FROM_TO"].ToString();
                    string costType = gvStatementBillingCredence.DataKeys[j]["COST_TYPE"].ToString();
                    string docID = gvStatementBillingCredence.DataKeys[j]["DOC_ID"].ToString();
                    decimal billingAmount = decimal.Parse(gvStatementBillingCredence.DataKeys[j]["BILLING_AMOUNT"].ToString());
                    bool exist = false;
                    for (int i = 0; i < billingCredenceListInfo.Count; i++)
                    {
                        if (((((billingCredenceListInfo[i].CredenceType == credenceType) && (billingCredenceListInfo[i].BillingObject == billingObject)) && ((billingCredenceListInfo[i].BillingObjectID == billingObjectID) && (billingCredenceListInfo[i].DueFromTo == dueFromTo))) && ((billingCredenceListInfo[i].CostType == costType) && (billingCredenceListInfo[i].DocID == docID))) && (billingCredenceListInfo[i].BillingAmount == billingAmount))
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (exist)
                    {
                        CheckBox myCheckBox = (CheckBox) gvStatementBillingCredence.Rows[j].FindControl("chbSelect");
                        myCheckBox.Checked = true;
                    }
                }
            }
        }

        private void SetStatementInfo2Form()
        {
            try
            {
                StatementInfo StatementInfo = BillingServiceClientProxy.GetInstance().GetStatementByID(this.hdnStatementID.Value);
                if (StatementInfo != null)
                {
                    this.txtStatementID.Text = StatementInfo.StatementID;
                    this.txtEndTime.Text = base.GetPreferenceShortDate(StatementInfo.EndTime);
                    this.txtRemark.Text = StatementInfo.Remark;
                    this.txtPaymentDate.Text = base.GetPreferenceShortDate(StatementInfo.PaymentDate);
                    this.ddlStatus.SelectedValue = StatementInfo.Status;
                    this.ddlDueFromTo.SelectedValue = StatementInfo.DueFromTo;
                    this.ddlBillingObject.SelectedValue = StatementInfo.BillingObject;
                    this.txtBillingObject.Text = StatementInfo.BillingObjectID;
                    this.txtTotalAmount.Text = StatementInfo.TotalAmount.ToString();
                    this.txtInvoiceAmount.Text = StatementInfo.InvoiceAmount.ToString();
                    this.txtPayAmount.Text = StatementInfo.PayAmount.ToString();
                    decimal CS00001 = StatementInfo.TotalAmount - StatementInfo.InvoiceAmount;
                    this.txtLastInvoiceAmount.Text = CS00001.ToString();
                    this.txtLastPayAmount.Text = (StatementInfo.InvoiceAmount - StatementInfo.PayAmount).ToString();
                    this.txtRemark.Text = StatementInfo.Remark;
                    this.txtStatementID.Enabled = false;
                    this.ddlDueFromTo.Enabled = false;
                    this.ddlBillingObject.Enabled = false;
                    this.txtBillingObject.Enabled = false;
                    this.txtTotalAmount.Enabled = false;
                    this.btnSearch.Enabled = false;
                    this.txtPaymentDate.Enabled = false;
                    this.txtEndTime.Enabled = false;
                    this.img1.Visible = false;
                    this.img2.Visible = false;
                    this.hdnFlag.Value = "1";
                    DataSet ds = BillingServiceClientProxy.GetInstance().GetBillingCredenceByStatementID(StatementInfo.StatementID);
                    base.BindData2GridView(this.gvStatementDetail, ds);
                    base.DataEntityViewState = ds;
                    this.Session["BillingCredenceListInfo"] = null;
                    this.gvStatementBillingCredence.DataSource = null;
                    this.gvStatementBillingCredence.DataBind();
                }
                else
                {
                    this.txtStatementID.Enabled = true;
                    this.ddlDueFromTo.Enabled = true;
                    this.ddlBillingObject.Enabled = true;
                    this.txtBillingObject.Enabled = true;
                    this.txtTotalAmount.Enabled = false;
                    this.txtPaymentDate.Enabled = false;
                    this.txtEndTime.Enabled = true;
                    this.txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    this.img1.Visible = true;
                    this.img2.Visible = true;
                    this.Session["BillingCredenceListInfo"] = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateStatementInfo(string Status)
        {
            this.lblMsg.Text = string.Empty;
            try
            {
                StatementInfo StatementInfo = new StatementInfo {
                    StatementID = this.txtStatementID.Text
                };
                if (!string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
                {
                    StatementInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                }
                StatementInfo.Remark = this.txtRemark.Text;
                StatementInfo.StatementDate = DateTime.Now;
                if (!string.IsNullOrEmpty(this.txtPaymentDate.Text.Trim()))
                {
                    StatementInfo.PaymentDate = DateTime.Now;
                }
                StatementInfo.CreatedBy = base.UserInfo.UserID;
                StatementInfo.DueFromTo = this.ddlDueFromTo.SelectedValue;
                StatementInfo.BillingObject = this.ddlBillingObject.SelectedValue;
                StatementInfo.BillingObjectID = this.txtBillingObject.Text.Trim();
                StatementInfo.Status = Status;
                StatementInfo.UpdatedDate = DateTime.Now;
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtTotalAmount.Text.Trim()))
                {
                    if (decimal.TryParse(this.txtTotalAmount.Text.Trim(), out tempDec))
                    {
                        StatementInfo.TotalAmount = decimal.Parse(this.txtTotalAmount.Text.Trim());
                    }
                    else
                    {
                        StatementInfo.TotalAmount = tempDec;
                    }
                }
                string CS40001 = Status;
                if ((CS40001 != null) && (CS40001 == "Reviewed"))
                {
                    StatementInfo.StatementDate = DateTime.Now;
                }
                this.lblMsg.Text = BillingServiceClientProxy.GetInstance().UpdateStatementStatus(StatementInfo) + base.GetGlobalMsgResource("F9200_005");
                this.SetStatementInfo2Form();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_006") + ex.Message;
            }
        }
    }
}

